package main

import (
	"encoding/json"
	"fmt"

	"github.com/360EntSecGroup-Skylar/excelize"
)

// Style 映射单元格样式设置。
type Style struct {
	Alignment     *Alignment `json:"alignment"`
	NumFmt        int        `json:"number_format"`
	DecimalPlaces int        `json:"decimal_places"`
	CustomNumFmt  *string    `json:"custom_number_format"`
	Lang          string     `json:"lang"`
	NegRed        bool       `json:"negred"`
}

type Alignment struct {
	Horizontal      string `json:"horizontal"`
	Indent          int    `json:"indent"`
	JustifyLastLine bool   `json:"justify_last_line"`
	ReadingOrder    uint64 `json:"reading_order"`
	RelativeIndent  int    `json:"relative_indent"`
	ShrinkToFit     bool   `json:"shrink_to_fit"`
	TextRotation    int    `json:"text_rotation"`
	Vertical        string `json:"vertical"`
	WrapText        bool   `json:"wrap_text"`
}

func CreateExcel() {
	f := excelize.NewFile()

	// 创建一个工作表
	index := f.NewSheet("MySheet")

	f.SetCellValue("MySheet", "A1", "Hello world.")

	// 设置工作簿的默认工作表
	f.SetActiveSheet(index)

	// 合并单元格
	// f.MergeCell("Sheet1", "D3", "E9")

	// 创建样式
	styleJson, _ := json.Marshal(&Style{
		Alignment: &Alignment{
			Horizontal: "center",
		},
	})
	style, _ := f.NewStyle(string(styleJson))

	// 设置单元格值
	f.SetCellValue("Sheet1", "A1", "用户名")
	f.SetCellValue("Sheet1", "B1", "账户")
	f.SetCellValue("Sheet1", "C1", "密码")
	f.SetCellValue("Sheet1", "A2", "管理员账户")

	// 设置样式
	f.SetCellStyle("Sheet1", "A1", "A1", style)

	// 根据指定路径保存文件
	if err := f.SaveAs("Learn_excelize.xlsx"); err != nil {
		fmt.Println(err)
	}
}

func ReadExcel() {
	f, _ := excelize.OpenFile("Learn_excelize.xlsx")
	rows, _ := f.Rows("Sheet1")
	for rows.Next() {
		row := rows.Columns()
		for _, colCell := range row {
			fmt.Print(colCell, "\t")
		}
		fmt.Println()
	}

}

func main() {
	// CreateExcel()
	ReadExcel()
}
